package com.zxc.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxc.gulimall.product.service.BrandService;
import com.zxc.gulimall.product.vo.BrandRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zxc.gulimall.product.entity.CategoryBrandRelationEntity;
import com.zxc.gulimall.product.service.CategoryBrandRelationService;
import com.zxc.common.utils.PageUtils;
import com.zxc.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author zxc
 * @email 2898743173@qq.com
 * @date 2021-10-26 20:06:35
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private BrandService brandService;
    /*
    * 获取当前分类
    * */
    //   /product/categorybrandrelation/brands/list
    @GetMapping(value = "/brands/list")
    public R getBrands(@RequestParam(value = "catId") Long catId){
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        if (catId != 0){
            queryWrapper.eq("catelog_id",catId);
        }
        //根据分类Id找出关系表里面的所有满足关系的数据
        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList = categoryBrandRelationService.list(queryWrapper);
        //遍历出所有的满足条件的brandId
        List<Long> collect = categoryBrandRelationEntityList.stream().map(brandId -> {
            return brandId.getBrandId();
        }).collect(Collectors.toList());

        //根据所有的Id找出所有符合要求的品牌数据，并且封装到brandRespVo
        List<BrandRespVo> brandRespVoList = brandService.selectBrandListByBrandId(collect);
        return R.ok().put("data",brandRespVoList);
    }

    /**
     * 获取当前品牌关联的所有分类列表
     */
    @GetMapping(value = "/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R catelogList(@RequestParam(value = "brandId")Long catelogId){
        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList =
                categoryBrandRelationService.
                        list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",catelogId));
        return R.ok().put("data", categoryBrandRelationEntityList);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /*
     * 保存当前品牌关联的所有分类列表
     * */
    @PostMapping("/save")
   // @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
